//
//  PhotoWall.m
//  PhotoDemo
//
//  Created by Harry on 12-12-6.
//  Copyright (c) 2012年 Harry. All rights reserved.
//

#import "HGPhotoWall.h"
#import "HGPhoto.h"

#define kScreenWidth [UIScreen mainScreen].bounds.size.width
//#define kFrameHeight ((kScreenWidth-85)/4.0+20)
//#define kFrameHeight2x ((kScreenWidth-85)/2.0+20)
//#define kFrameHeight3x ((kScreenWidth-85)/2+(kScreenWidth-85)/4.0+40)


#define kImagePositionx @"positionx"
#define kImagePositiony @"positiony"

@interface HGPhotoWall() <HGPhotoDelegate>
{
    NSInteger _leadTailSpace; //照片墙前后空格的宽度
    NSInteger _middleSpace; //照片中间空格的宽度
    LayoutPhotosType _photoWallType; //这个照片墙的type
    CGFloat kFrameHeight3x;
    ZYPhotosType _ZYHGphotoType;
}
@property (strong, nonatomic) UILabel *labelDescription;

@property (strong, nonatomic) NSArray *arrayPositions;
//@property (strong, nonatomic) NSMutableArray *arrayPhotos;
@property (nonatomic) BOOL isEditModel;
@property (nonatomic, assign) CGPoint photoPoint;

@end


@implementation HGPhotoWall

/*
 @LayoutPhotosType
 Ten:_spaceWidth = 60+5*5;
 Tewlve:_spaceWidth = 20+10*3+20;
 */
- (id)initWithFrame:(CGRect)frame andLayoutPhotoType:(LayoutPhotosType)type
{
//    self = [super initWithFrame:CGRectMake(0., 0., kScreenWidth-70, 0.)];
    _photoWallType = type;
    if (type == LayoutTenPhotos) {
        /*原来的布局
        self = [super initWithFrame:CGRectMake(0., 0., kScreenWidth-70, 0.)];
        _leadTailSpace = 70;
        _middleSpace = 5;
         */
        self = [super initWithFrame:CGRectMake(0., 0., kScreenWidth, 0.)];
        _leadTailSpace = 40;
        _middleSpace = 10;
        _ZYHGphotoType = ZYPhotosTen;
    }
    else
    {
        self = [super initWithFrame:CGRectMake(0., 0., kScreenWidth, 0.)];
        _leadTailSpace = 40;
        _middleSpace = 10;
        _ZYHGphotoType = ZYPhotosTwelve;
    }
    CGFloat spaceWidth = _leadTailSpace + _middleSpace*3;
    kFrameHeight3x = (kScreenWidth- spaceWidth)*1.0/2+(kScreenWidth-spaceWidth)*1.0/4.0+40;
    _photoPoint = frame.origin;
    /*
    NSInteger headSpace;
    if (type == LayoutTenPhotos) {
        headSpace = 85;
    }
    else
    {
        headSpace = 25;
    }
     */
    CGFloat picWidth=(kScreenWidth-spaceWidth)/4.0;
    CGFloat picWidthAndBlock = picWidth+_middleSpace;
    
    NSString *xPosition2 = [NSString stringWithFormat:@"%f", picWidthAndBlock+4];
    NSString *xPosition3 = [NSString stringWithFormat:@"%f", picWidthAndBlock*2+4];
    NSString *xPosition4 = [NSString stringWithFormat:@"%f", picWidthAndBlock*3+4];
    NSString *yPosition2 = [NSString stringWithFormat:@"%f", picWidthAndBlock+10];
    NSString *yPosition3 = [NSString stringWithFormat:@"%f", picWidthAndBlock*2+10];

    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        if (type == LayoutTenPhotos) {
            self.arrayPositions = [NSArray arrayWithObjects:
                                   [NSDictionary dictionaryWithObjectsAndKeys:@"4", kImagePositionx, @"10", kImagePositiony, nil],
                                   [NSDictionary dictionaryWithObjectsAndKeys:xPosition2, kImagePositionx, @"10", kImagePositiony, nil],
                                   [NSDictionary dictionaryWithObjectsAndKeys:xPosition3, kImagePositionx, @"10", kImagePositiony, nil],
                                   [NSDictionary dictionaryWithObjectsAndKeys:xPosition4, kImagePositionx, @"10", kImagePositiony, nil],
                                   [NSDictionary dictionaryWithObjectsAndKeys:@"4", kImagePositionx, yPosition2, kImagePositiony, nil],
                                   [NSDictionary dictionaryWithObjectsAndKeys:xPosition2, kImagePositionx, yPosition2, kImagePositiony, nil],
                                   [NSDictionary dictionaryWithObjectsAndKeys:xPosition3, kImagePositionx, yPosition2, kImagePositiony, nil],
                                   [NSDictionary dictionaryWithObjectsAndKeys:xPosition4, kImagePositionx, yPosition2, kImagePositiony, nil],
                                   [NSDictionary dictionaryWithObjectsAndKeys:@"4", kImagePositionx, yPosition3, kImagePositiony, nil],
                                   [NSDictionary dictionaryWithObjectsAndKeys:xPosition2, kImagePositionx, yPosition3, kImagePositiony, nil],
                                   [NSDictionary dictionaryWithObjectsAndKeys:@"415", kImagePositionx, yPosition3, kImagePositiony, nil],nil];
        }
        else if(type == LayoutTwelfPhotos)
        {
            self.arrayPositions = [NSArray arrayWithObjects:
                                   [NSDictionary dictionaryWithObjectsAndKeys:@"4", kImagePositionx, @"10", kImagePositiony, nil],
                                   [NSDictionary dictionaryWithObjectsAndKeys:xPosition2, kImagePositionx, @"10", kImagePositiony, nil],
                                   [NSDictionary dictionaryWithObjectsAndKeys:xPosition3, kImagePositionx, @"10", kImagePositiony, nil],
                                   [NSDictionary dictionaryWithObjectsAndKeys:xPosition4, kImagePositionx, @"10", kImagePositiony, nil],
                                   [NSDictionary dictionaryWithObjectsAndKeys:@"4", kImagePositionx, yPosition2, kImagePositiony, nil],
                                   [NSDictionary dictionaryWithObjectsAndKeys:xPosition2, kImagePositionx, yPosition2, kImagePositiony, nil],
                                   [NSDictionary dictionaryWithObjectsAndKeys:xPosition3, kImagePositionx, yPosition2, kImagePositiony, nil],
                                   [NSDictionary dictionaryWithObjectsAndKeys:xPosition4, kImagePositionx, yPosition2, kImagePositiony, nil],
                                   [NSDictionary dictionaryWithObjectsAndKeys:@"4", kImagePositionx, yPosition3, kImagePositiony, nil],
                                   [NSDictionary dictionaryWithObjectsAndKeys:xPosition2, kImagePositionx, yPosition3, kImagePositiony, nil],
                                   [NSDictionary dictionaryWithObjectsAndKeys:xPosition3, kImagePositionx, yPosition3, kImagePositiony, nil],
                                   [NSDictionary dictionaryWithObjectsAndKeys:xPosition4, kImagePositionx, yPosition3, kImagePositiony, nil],
                                   [NSDictionary dictionaryWithObjectsAndKeys:@"415", kImagePositionx, yPosition3, kImagePositiony, nil],nil];
        }
        self.arrayPhotos = [NSMutableArray arrayWithCapacity:1];
        self.labelDescription = [[UILabel alloc] initWithFrame:CGRectMake(10., 0., kScreenWidth-90, 18.)];
        self.labelDescription.backgroundColor = [UIColor clearColor];
        self.labelDescription.textColor = [UIColor whiteColor];
        self.labelDescription.font = [UIFont systemFontOfSize:12.];
        self.labelDescription.textAlignment = NSTextAlignmentLeft;
//      [self addSubview:self.labelDescription];
        self.labelDescription.hidden = YES;
//        self.labelDescription.text = @"最多上传9张图片";
    }
    return self;
}

/*
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:CGRectMake(0., 0., 320., 0.)];
    _photoPoint = frame.origin;
    if (self) {
        self.backgroundColor = [UIColor darkGrayColor];
        self.arrayPositions = [NSArray arrayWithObjects:
                               [NSDictionary dictionaryWithObjectsAndKeys:@"4", kImagePositionx, @"10", kImagePositiony, nil],
                               [NSDictionary dictionaryWithObjectsAndKeys:@"83", kImagePositionx, @"10", kImagePositiony, nil],
                               [NSDictionary dictionaryWithObjectsAndKeys:@"162", kImagePositionx, @"10", kImagePositiony, nil],
                               [NSDictionary dictionaryWithObjectsAndKeys:@"241", kImagePositionx, @"10", kImagePositiony, nil],
                               [NSDictionary dictionaryWithObjectsAndKeys:@"4", kImagePositionx, @"90", kImagePositiony, nil],
                               [NSDictionary dictionaryWithObjectsAndKeys:@"83", kImagePositionx, @"90", kImagePositiony, nil],
                               [NSDictionary dictionaryWithObjectsAndKeys:@"162", kImagePositionx, @"90", kImagePositiony, nil],
                               [NSDictionary dictionaryWithObjectsAndKeys:@"241", kImagePositionx, @"90", kImagePositiony, nil],
                               [NSDictionary dictionaryWithObjectsAndKeys:@"330", kImagePositionx, @"90", kImagePositiony, nil], nil];
        self.arrayPhotos = [NSMutableArray arrayWithCapacity:1];
        self.labelDescription = [[UILabel alloc] initWithFrame:CGRectMake(10., 0., 300., 18.)];
        self.labelDescription.backgroundColor = [UIColor clearColor];
        self.labelDescription.textColor = [UIColor whiteColor];
        self.labelDescription.font = [UIFont systemFontOfSize:12.];
        self.labelDescription.textAlignment = NSTextAlignmentLeft;
        //      [self addSubview:self.labelDescription];
        self.labelDescription.hidden = YES;
        self.labelDescription.text = @"拖拽图片可以排列顺序, 点击添加照片.";
    }
    return self;
}
*/

- (void)setPhotos:(NSArray*)photos
{
    [self.arrayPhotos removeAllObjects];
    NSUInteger count = [photos count];
    for (int i=0; i<count; i++) {
        NSDictionary *dictionaryTemp = [self.arrayPositions objectAtIndex:i];
        CGFloat originx = [[dictionaryTemp objectForKey:kImagePositionx] floatValue];
        CGFloat originy = [[dictionaryTemp objectForKey:kImagePositiony] floatValue];
        HGPhoto *photoTemp = [[HGPhoto alloc] initWithOrigin:CGPointMake(originx, originy) photoType:_ZYHGphotoType];
        photoTemp.delegate = self;
        [photoTemp setPhotoUrl:[photos objectAtIndex:i]];
        [self addSubview:photoTemp];
        [self.arrayPhotos addObject:photoTemp];
    }
    
    NSDictionary *dictionaryTemp = [self.arrayPositions objectAtIndex:count];
    CGFloat originx = [[dictionaryTemp objectForKey:kImagePositionx] floatValue];
    CGFloat originy = [[dictionaryTemp objectForKey:kImagePositiony] floatValue];
    HGPhoto *photoTemp = [[HGPhoto alloc] initWithOrigin:CGPointMake(originx, originy) photoType:_ZYHGphotoType];
    photoTemp.delegate = self;
    photoTemp.hidden = NO;
    [photoTemp setPhotoType:PhotoTypeAdd];
    [self.arrayPhotos addObject:photoTemp];
    [self addSubview:photoTemp];
    
    CGFloat frameHeight = -1;
    
    /*
    if (count > 3)
    {
        frameHeight = kFrameHeight2x;
    }
    else
    {
        frameHeight = kFrameHeight;
    }*/
    frameHeight = kFrameHeight3x;
    if (_photoWallType == LayoutTenPhotos) {
        self.frame = CGRectMake(0., 0., kScreenWidth-70, frameHeight);
    }
    else
    {
        self.frame = CGRectMake(0., 0., kScreenWidth, frameHeight);
    }
#pragma mark - 修改部分
//    self.frame = CGRectMake(0., 0., kScreenWidth, frameHeight);
//    self.frame = CGRectMake(_photoPoint.x, _photoPoint.y, 320., frameHeight);

}

- (void)setEditModel:(BOOL)canEdit
{
    self.isEditModel = canEdit;
    if (self.isEditModel) {
        HGPhoto *viewTemp = [self.arrayPhotos lastObject];
        viewTemp.hidden = NO;
        self.labelDescription.hidden = NO;
    } else {
        HGPhoto *viewTemp = [self.arrayPhotos lastObject];
        viewTemp.hidden = YES;
        self.labelDescription.hidden = YES;
    }
    
    NSUInteger count = [self.arrayPhotos count]-1;
    for (int i=0; i<count; i++) {
        HGPhoto *viewTemp = [self.arrayPhotos objectAtIndex:i];
        [viewTemp setEditModel:self.isEditModel];
    }
    [self reloadPhotos:NO];
}

- (void)addPhoto:(NSString*)string
{
    NSUInteger index = [self.arrayPhotos count] - 1;
    NSDictionary *dictionaryTemp = [self.arrayPositions objectAtIndex:index];
    CGFloat originx = [[dictionaryTemp objectForKey:kImagePositionx] floatValue];
    CGFloat originy = [[dictionaryTemp objectForKey:kImagePositiony] floatValue];
    
    HGPhoto *photoTemp = [[HGPhoto alloc] initWithOrigin:CGPointMake(originx, originy) photoType:_ZYHGphotoType];
    photoTemp.delegate = self;
    [photoTemp setPhotoUrl:string];
    
    [self.arrayPhotos insertObject:photoTemp atIndex:index];
    [self addSubview:photoTemp];
    [self reloadPhotos:YES];
}

- (void)addLocalPhoto:(UIImage *)image
{
    NSUInteger index = [self.arrayPhotos count] - 1;
    NSDictionary *dictionaryTemp = [self.arrayPositions objectAtIndex:index];
    CGFloat originx = [[dictionaryTemp objectForKey:kImagePositionx] floatValue];
    CGFloat originy = [[dictionaryTemp objectForKey:kImagePositiony] floatValue];
    
    HGPhoto *photoTemp = [[HGPhoto alloc] initWithOrigin:CGPointMake(originx, originy) photoType:_ZYHGphotoType];
    photoTemp.delegate = self;
    [photoTemp setPhotoImage:image];
    
    [self.arrayPhotos insertObject:photoTemp atIndex:index];
    [self addSubview:photoTemp];
    [self reloadPhotos:YES];

}

- (void)deletePhotoByIndex:(NSUInteger)index
{
    HGPhoto *photoTemp = [self.arrayPhotos objectAtIndex:index];
    [self.arrayPhotos removeObject:photoTemp];
    [photoTemp removeFromSuperview];
    [self reloadPhotos:YES];
}

#pragma mark - Photo

- (void)photoTaped:(HGPhoto*)photo
{
    NSUInteger type = [photo getPhotoType];
    if (type == PhotoTypeAdd) {
        if ([self.delegate respondsToSelector:@selector(photoWallAddAction)]) {
            [self.delegate photoWallAddAction];
        }
    } else if (type == PhotoTypePhoto) {
        NSUInteger index = [self.arrayPhotos indexOfObject:photo];
        if ([self.delegate respondsToSelector:@selector(photoWallPhotoBrowser:)]) {
            [self.delegate photoWallPhotoBrowser:index];
        }
    }
}

- (void)photoDeleteAction:(HGPhoto *)photo
{
    NSUInteger index = [self.arrayPhotos indexOfObject:photo];
    if ([self.delegate respondsToSelector:@selector(photoWallPhotoTaped:)]) {
        [self.delegate photoWallPhotoTaped:index];
    }
}

- (void)photoMoveFinished:(HGPhoto*)photo
{
    CGPoint pointPhoto = CGPointMake(photo.frame.origin.x, photo.frame.origin.y);
    CGFloat space = -1;
    NSUInteger oldIndex = [self.arrayPhotos indexOfObject:photo];
    NSUInteger newIndex = -1;
    
    NSUInteger count = [self.arrayPhotos count] - 1;
    for (int i=0; i<count; i++) {
        NSDictionary *dictionaryTemp = [self.arrayPositions objectAtIndex:i];
        CGFloat originx = [[dictionaryTemp objectForKey:kImagePositionx] floatValue];
        CGFloat originy = [[dictionaryTemp objectForKey:kImagePositiony] floatValue];
        CGPoint pointTemp = CGPointMake(originx, originy);
        CGFloat spaceTemp = [self spaceToPoint:pointPhoto FromPoint:pointTemp];
        if (space < 0) {
            space = spaceTemp;
            newIndex = i;
        } else {
            if (spaceTemp < space) {
                space = spaceTemp;
                newIndex = i;
            }
        }
    }
    
    [self.arrayPhotos removeObject:photo];
    [self.arrayPhotos insertObject:photo atIndex:newIndex];
    
    [self reloadPhotos:NO];
    
    if ([self.delegate respondsToSelector:@selector(photoWallMovePhotoFromIndex:toIndex:)]) {
        [self.delegate photoWallMovePhotoFromIndex:oldIndex toIndex:newIndex];
    }
}

- (void)reloadPhotos:(BOOL)add
{
    NSUInteger count = -1;
    if (add) {
        count = [self.arrayPhotos count];
    } else {
        count = [self.arrayPhotos count] - 1;
    }
    for (int i=0; i<count; i++) {
        NSDictionary *dictionaryTemp = [self.arrayPositions objectAtIndex:i];
        CGFloat originx = [[dictionaryTemp objectForKey:kImagePositionx] floatValue];
        CGFloat originy = [[dictionaryTemp objectForKey:kImagePositiony] floatValue];
        
        HGPhoto *photoTemp = [self.arrayPhotos objectAtIndex:i];
        [photoTemp moveToPosition:CGPointMake(originx, originy)];
    }
    
    CGFloat frameHeight = -1;
    NSUInteger countPhoto = [self.arrayPhotos count];
    if (self.isEditModel) {
        /*
        if (countPhoto > 3) {
            frameHeight = kFrameHeight2x + 20.;
        } else {
            frameHeight = kFrameHeight + 20.;
        }
         */
        frameHeight = kFrameHeight3x+20;
        self.labelDescription.frame = CGRectMake(self.labelDescription.frame.origin.x, frameHeight - 20., self.labelDescription.frame.size.width, self.labelDescription.frame.size.height);
    } else {
        /*
        if (countPhoto > 5) {
            frameHeight = kFrameHeight2x;
        } else {
            frameHeight = kFrameHeight;
        }
         */
        frameHeight = kFrameHeight3x+20;
    }
//    self.frame = CGRectMake(0., 0., kScreenWidth-70, frameHeight);
    if (_photoWallType == LayoutTenPhotos) {
        self.frame = CGRectMake(0., 0., kScreenWidth-70, frameHeight);
    }
    else
    {
        self.frame = CGRectMake(0., 0., kScreenWidth, frameHeight);
    }

#pragma mark - 修改部分
//    self.frame = CGRectMake(0., 0., kScreenWidth-70, frameHeight);

}

- (CGFloat)spaceToPoint:(CGPoint)point FromPoint:(CGPoint)otherPoint
{
    float x = point.x - otherPoint.x;
    float y = point.y - otherPoint.y;
    return sqrt(x * x + y * y);
}

@end
